package com.dh.weibo.controller;

import com.dh.weibo.application.Application;
import com.dh.weibo.dao.CollectionDao;
import com.dh.weibo.dao.CommentDao;
import com.dh.weibo.dao.LikeDao;
import com.dh.weibo.dao.WeiBoDao;
import com.dh.weibo.pojo.Collection;
import com.dh.weibo.pojo.Comment;
import com.dh.weibo.pojo.Like;
import com.dh.weibo.pojo.WeiBo;
import com.dh.weibo.pojo.vo.WeiBoVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WeiBoController {

    private WeiBoDao weiBoDao = new WeiBoDao();
    private CommentDao commentDao = new CommentDao();
    private LikeDao likeDao = new LikeDao();
    private CollectionDao collectionDao = new CollectionDao();


    /**
     * 发布微博
     *
     * @param title   标题
     * @param content 内容
     */
    public void publish(String title, String content) {
        WeiBo weiBo = new WeiBo(title, content, Application.user.getUid());
        weiBoDao.publish(weiBo);
    }

    /**
     * 返回所有微博
     *
     * @return
     */
    public List<WeiBo> getWeiBos() {
        return weiBoDao.getWeiBos();
    }

    /**
     * 获取微博和对应的评论
     *
     * @return
     */
//    public HashMap<WeiBo, List<Comment>> getWeiBosAndComments() {
//        List<WeiBo> weiBos = weiBoDao.getWeiBos();// 拿到所有微博
//        HashMap<WeiBo, List<Comment>> weiBoListHashMap = new HashMap<>();
//        for (WeiBo weiBo : weiBos) {
//            List<Comment> comments = commentDao.getCommentsByWeiBoID(weiBo.getId());// 根据微博ID获取响应评论
//            System.out.println("评论的个数" + comments.size());
//            weiBoListHashMap.put(weiBo, comments);
//        }
//        return weiBoListHashMap;
//    }
    public List<WeiBoVo> getWeiBosAndComments() {
        List<WeiBo> weiBos = weiBoDao.getWeiBos();// 拿到所有微博
        List<WeiBoVo> weiBoVos = new ArrayList<>();
        for (WeiBo weiBo : weiBos) {
            List<Comment> comments = commentDao.getCommentsByWeiBoID(weiBo.getId());// 根据微博ID获取响应评论
            WeiBoVo weiBoVo = new WeiBoVo(weiBo, comments);//封装  一个对象
            weiBoVos.add(weiBoVo);
        }
        return weiBoVos;
    }


    /**
     * 评论
     *
     * @param content
     * @param id
     */
    public void comment(String content, String id) {
        WeiBo weiBo = weiBoDao.getWeiBoByID(id);
        if (weiBo != null) {// 只有 有这条微博，才能发表评论
            Comment comment = new Comment(Application.user.getUid(), content, id);
            commentDao.comment(comment);
            int count = weiBo.getComment().intValue();
            weiBo.setComment(++count);
        }

    }

    /**
     * 点赞
     *
     * @param id 微博ID
     */
    public void like(String id) {
        Like like = new Like(Application.user.getUid(), id);
        likeDao.like(like);
    }

    /**
     * 收藏
     *
     * @param id
     */
    public void collection(String id) {
        Collection collection = new Collection(Application.user.getUid(), id);
        collectionDao.collection(collection);
    }

    /**
     * 返回自己的评论的微博
     *
     * @return
     */
    public List<Comment> getComments() {
        List<Comment> currentComment = new ArrayList<>();
        List<Comment> comments = commentDao.getComments();
        if (Application.user.isSuper()) {// 如果当前用户是管理员
            return comments;
        }
        for (Comment comment : comments) {
            if (comment.getUid().equals(Application.user.getUid())) {
                currentComment.add(comment);
            }
        }
        return currentComment;
    }

    /**
     * 返回自己点赞过的微博
     *
     * @return
     */
    public List<Like> getLikes() {

        return null;
    }

    /**
     * 返回自己收藏的微博
     */
    public List<Collection> getCollections() {

        return null;
    }

    public void delete(String id) {
        /*
            1、通过id查找对应的微博
            2、删除微博
            3、删除微博的评论、收藏、点赞
         */
        WeiBo weiBo = weiBoDao.getWeiBoByID(id);
        if (weiBo != null) {
            weiBoDao.delete(weiBo);// 删除
            commentDao.deleteCommentsByWeiBoID(weiBo.getId());// 删除评论
            likeDao.delete(weiBo.getId());// 删除点赞
            collectionDao.delete(weiBo.getId());// 删除评论
        }

    }

    /**
     * 根据ID删除评论
     *
     * @param cid
     */
    public void deleteComment(String cid) {
        Comment comment = commentDao.getCommentByID(cid);// 先获取评论
        if (comment != null) {
            commentDao.deleteCommentById(cid);// 删除对应的评论
            // 根据评论里面存放的微博ID找到对应的微博
            WeiBo weiBo = weiBoDao.getWeiBoByID(comment.getId());
            if (weiBo != null) {
                Integer count = weiBo.getComment();
                weiBo.setComment(--count);//减一
            }

        }
    }
}
